<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>注册</title>
  <link rel="stylesheet" href="css/auth.css">
</head>
<body>
  <div class="container">
    <h2>注册</h2>
    <form id="register-form">
      <input type="text" id="username" placeholder="用户名" required />
      <input type="email" id="email" placeholder="邮箱" required />
      <input type="password" id="password" placeholder="密码" required />
      <button type="submit">注册</button>
    </form>
    <p>已有账号？<a href="login.html">登录</a></p>
  </div>

  <script>
    document.getElementById('register-form').addEventListener('submit', async function (e) {
      e.preventDefault();

      const username = document.getElementById('username').value;
      const email = document.getElementById('email').value;
      const password = document.getElementById('password').value;

      try {
        const response = await fetch('http://localhost:3000/api/register', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({ username, email, password })
        });

        const data = await response.json();
        if (response.ok) {
          alert('注册成功！');
          window.location.href = 'login.html'; // 注册成功后跳转到登录页面
        } else {
          alert('注册失败: ' + data.message);
        }
      } catch (error) {
        alert('注册请求失败: ' + error.message);
      }
    });
  </script>
</body>
</html>
